package com.educatioin.manage.web.system.controller;

import com.educatioin.commons.util.AjaxResult;
import com.educatioin.manage.web.schoolgradeclass.po.SchoolGradeClassPo;
import com.educatioin.manage.web.system.po.CustomerPo;
import com.educatioin.manage.web.system.request.CreateCustomerReq;
import com.educatioin.manage.web.system.request.ListCustomerReq;
import com.educatioin.manage.web.system.request.ModifyCustomerReq;
import com.educatioin.manage.web.system.response.ListCustomerRsp;
import com.educatioin.manage.web.system.service.CustomerService;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 客户管理模块
 */
@RestController
@RequestMapping(value = {"/api/customer"})
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @RequestMapping("/list")
    @ResponseBody
    public ListCustomerRsp list(ListCustomerReq req) {
        return customerService.list(req);
    }

    @RequestMapping("/modify")
    @ResponseBody
    public AjaxResult modify(@RequestBody ModifyCustomerReq req) {
        AjaxResult result = new AjaxResult<>();
        customerService.modify(req);
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("modify success");
        return result;
    }

    @RequestMapping("/create")
    @ResponseBody
    public AjaxResult create(@RequestBody CreateCustomerReq req) {
        AjaxResult result = new AjaxResult<>();
        customerService.create(req);
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("modify success");
        return result;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public AjaxResult findById(Long id) {
        AjaxResult result = new AjaxResult<>();
        result.setData(customerService.findById(id));
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("success");
        return result;
    }

    @RequestMapping("/listSelectOptions")
    @ResponseBody
    public AjaxResult listSelectOptions() {
        AjaxResult result = new AjaxResult<>();
        List<Map<String, Object>> schoolGradeClassList = Lists.newArrayList();
        Iterable<CustomerPo> list = customerService.getCustomerRepository().findAll();
        if (list != null) {
            list.forEach(s -> {
                Map<String, Object> cust = Maps.newHashMap();
                cust.put("id", s.getId());
                cust.put("name", s.getName());
                schoolGradeClassList.add(cust);
            });
        }
        result.setData(schoolGradeClassList);
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("success");
        return result;
    }
}
